/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxinline.servico;

import br.com.foxinline.enums.TipoProfissional;
import br.com.foxinline.modelo.Profissional;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author ely
 */
@Stateless
public class ProfissionalServico extends ServicoGenerico<Profissional> {

    public ProfissionalServico() {
        super(Profissional.class);
    }

    public List<Profissional> findProfissional(Profissional profissional) {
        String sql = "select p from Profissional p ";

        sql += " WHERE ";

        if (profissional.getNome() != null && !profissional.getNome().equals("")) {
            sql += " lower(p.nome) like lower(:nome) and ";
        }

        if (profissional.getCpf() != null && !profissional.getCpf().equals("")) {
            sql += " lower(p.cpf) like lower(:cpf) and ";
        }
        if (profissional.getNumeroCartaoSus() != null && !profissional.getNumeroCartaoSus().equals("")) {
            sql += " lower(p.numeroCartaoSus) like lower(:numSus) and ";
        }

        if (profissional.getTipoProfissional() != null) {
            sql += " p.tipoProfissional  = :tipoProfissional and ";

        }

        sql += " 1=1 AND p.ativo = TRUE Order by p.nome ASC";

        Query query = getEntityManager().createQuery(sql);

        if (profissional.getNome() != null && !profissional.getNome().equals("")) {
            query.setParameter("nome", "%" + profissional.getNome() + "%");
        }

        if (profissional.getCpf() != null && !profissional.getCpf().equals("")) {
            query.setParameter("cpf", profissional.getCpf());
        }

        if (profissional.getNumeroCartaoSus() != null && !profissional.getNumeroCartaoSus().equals("")) {
            query.setParameter("numeroCartaoSus", profissional.getNumeroCartaoSus());
        }

        if (profissional.getTipoProfissional() != null) {
            query.setParameter("tipoProfissional", profissional.getTipoProfissional());
        }

        return query.getResultList();
    }

    public Long QtdProfissional(TipoProfissional tipo) {
        String sql = "SELECT count(p.id) FROM Profissional p JOIN p.tipoProfissional t WHERE t = :tipo";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("tipo", tipo);
        return (Long) query.getSingleResult();
    }

    public List<Profissional> profissionalTipoProfissional(TipoProfissional tipo) {
        String sql = "SELECT p FROM Profissional p JOIN p.tipoProfissional t WHERE t = :tipo";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("tipo", tipo);
        return query.getResultList();
    }
}
